package com.zaoqi.consumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/consumer")
public class RestTemplateController {

    @Autowired
    private RestTemplate restTemplate;

    private final String providerUrl = "http://provider-service/api/provider";


    @GetMapping("/get/{id}")
    public String getData(@PathVariable Integer id) {
        System.out.println(restTemplate.getForObject(providerUrl + "/" + id, String.class));
        return restTemplate.getForObject(providerUrl + "/" + id, String.class);
    }

    @PostMapping("/post")
    public String postData(@RequestParam Integer id, @RequestParam String value) {
        return restTemplate.postForObject(providerUrl + "?id=" + id + "&value=" + value, null, String.class);
    }

    @PutMapping("/put")
    public String putData(@RequestParam Integer id, @RequestParam String value) {
        restTemplate.put(providerUrl + "?id=" + id + "&value=" + value, null);
        return "PUT 请求已发送";
    }

    @DeleteMapping("/delete/{id}")
    public String deleteData(@PathVariable Integer id) {
        restTemplate.delete(providerUrl + "/" + id);
        return "DELETE 请求已发送";
    }
}

